# -*- coding: utf-8 -*-

'''eof
name:申请企业账户状态校验
code:R68_EntAccountStatusCheck_ZC
tableName:
columnName:
groups:企业业务信息校验
dependencies:EG_DSJ
type:常用指标
datasourceType:在线指标
description:
eof'''

import sys
import json

reload(sys)
sys.setdefaultencoding('utf-8')


def getRule68():
    null_type_list = ['', ' ', None, 'null', 'Null', 'NULL', '/', '-', '--']

    entAccountInfo = json.loads(ratingRequest.get("entAccountInfo"))
    ClientClassList = ["3", "4"]
    AcctStateList = ["A", "N"]
    resultList = []
    if entAccountInfo not in null_type_list and isinstance(entAccountInfo, list):
        if len(entAccountInfo) >= 1:
            for i in entAccountInfo:
                try:
                    ClientType = i.get("ClientType")
                    AcctState = i.get("AcctState")
                    ClientClass = i.get("ClientClass")
                    IsTheSameOrg = i.get("IsTheSameOrg")
                    IsInterNetAcct = i.get("IsInterNetAcct")
                    if IsTheSameOrg == "1":
                        resultList.append(1)
                    elif IsTheSameOrg == "0":
                        if ClientType not in null_type_list and ClientType == "02" and ClientClass not in null_type_list \
                                and ClientClass in ClientClassList and AcctState not in null_type_list and AcctState in AcctStateList:
                            if IsInterNetAcct not in null_type_list:
                                if IsInterNetAcct == "1":
                                    resultList.append(2)
                                elif IsInterNetAcct == "0":
                                    resultList.append(3)
                        else:
                            resultList.append(0)

                except:
                    continue
            if 3 in resultList:
                return 3
            elif 2 in resultList:
                return 2
            elif 0 in resultList:
                return 0
            elif 1 in resultList:
                return 1
            else:
                raise ValueError("网贷传输的数据字段缺失或不符合要求")
        else:
            return 1
    else:
        raise RuntimeError("网贷传输的数据企业账户信息错误或者为空")


if __name__ == "__main__":
    file_obj = open(r'../data/rating/R04_EG_IndLimitCheck/CQB_DSJ.json', 'r')
    content = file_obj.read()
    CQB_DSJ = json.loads(content, strict=False)
    aa = getRule68()
    print aa

result = getRule68()
